//
//  famous_groups.m
//  VivaModa
//
//  Created by Owly MacBook on 06.02.13.
//  Copyright (c) 2013 private_busines. All rights reserved.
//

#import "famous_groups.h"
#import "EGOPhotoGlobal.h"
#import "MyPhoto.h"
#import "MyPhotoSource.h"
#import "famous_cell.h"


@interface famous_groups ()

@end

@implementation famous_groups
@synthesize tableView;
@synthesize famous_group_data;
@synthesize photos = _photos;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


-(void)getDataFromDB{
    famous_group_data = [SQLiteAccess selectManyRowsWithSQL:@"SELECT * FROM famous"];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self getDataFromDB];
    [self setNavigationTitle];
    [self addCustomBackButton];
    self.view.backgroundColor = [Singleton sharedMySingleton].backgroundTableView;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [famous_group_data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"famous_cell";
    
    
    
    famous_cell *cell = nil;
    cell = (famous_cell *) [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if(!cell)
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"famous_cell" owner:nil options:nil];
        for(id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[famous_cell class]])
            {
                cell = (famous_cell *)currentObject;
                break;
            }
        }
    }
       
    cell.name_group.text = [[famous_group_data objectAtIndex:indexPath.row] objectForKey:@"name_group"];
    [cell.name_group setFont:[UIFont fontWithName:@"Century Gothic" size:17.0]];
    
    cell.name_group.highlightedTextColor = [UIColor purpleColor];
    
    
    UIView *selectedView = [[UIView alloc] init];
    selectedView.backgroundColor = [UIColor clearColor];
    cell.selectedBackgroundView = selectedView;
    
    return cell;
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = [[famous_group_data objectAtIndex:indexPath.row] objectForKey:@"name_group"];
    UIFont *cellFont = [UIFont fontWithName:@"Century Gothic" size:17.0];
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    
    CGSize labelSize = [cellText sizeWithFont:cellFont
                            constrainedToSize:constraintSize
                                lineBreakMode:UILineBreakModeWordWrap];
    
    return labelSize.height + 20.0f+ 20;
}



#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSArray *image_data = [SQLiteAccess selectManyRowsWithSQL:[NSString stringWithFormat:@"SELECT * FROM famous_images WHERE id_famous=%@",[[famous_group_data objectAtIndex:indexPath.row] objectForKey:@"id"]]];
    
    
   // NSLog(@"%@",image_data);
    
    NSMutableArray *array_img = [[NSMutableArray alloc] initWithCapacity:1];
    [array_img removeAllObjects];
    for (int i = 0; i < [image_data count]; i++) {
        MyPhoto *photo = [[MyPhoto alloc] initWithImageURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[image_data objectAtIndex:i] objectForKey:@"image"]]] name:[[image_data objectAtIndex:i] objectForKey:@"description"]];
        [array_img addObject:photo];
    }
    
    
    MyPhotoSource *source = [[MyPhotoSource alloc] initWithPhotos:array_img];
    
    EGOPhotoViewController *photoController = [[EGOPhotoViewController alloc] initWithPhotoSource:source];
    [self.navigationController pushViewController:photoController animated:YES];
    
}

-(void)setNavigationTitle{
    UILabel *lblTitle = [[UILabel alloc] init];
    lblTitle.text = @" Знаменитости";
    lblTitle.backgroundColor = [UIColor clearColor];
    lblTitle.textColor = [UIColor colorWithRed:242.0/255.0 green:240.0/255.0 blue:234.0/255.0 alpha:1];
    
    lblTitle.font = [UIFont fontWithName:@"American Retro" size:27.0];
    [lblTitle sizeToFit];
    
    self.navigationItem.titleView = lblTitle;
}



-(void)addCustomBackButton{
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 71, 29)];
    //[backButton setImage:[[UIImage imageNamed:@"img_back_btn.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10) ] forState:UIControlStateNormal];
    
    [backButton setBackgroundImage:[[UIImage imageNamed:@"img_back_btn.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(8, 20, 20, 20) ] forState:UIControlStateNormal];
    
    [backButton setTitle:@" Инфо" forState:UIControlStateNormal];
    [backButton.titleLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12]];
    
    [backButton setShowsTouchWhenHighlighted:NO];
    [backButton addTarget:self action:@selector(popViewControllerWithAnimation) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barBackItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.hidesBackButton = TRUE;
    self.navigationItem.leftBarButtonItem = barBackItem;
}

-(void)popViewControllerWithAnimation{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
